﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RestFullChatAJAX.Models;
using RestFullChatAJAX.Models.DAO;
using Newtonsoft.Json.Linq;

namespace RestFullChatAJAX.Controllers
{
    public class FriendController : ApiController
    {
        // POST api/Friend/username?userreceive
        public String PostFriendSubmit(String id, [FromUri]String userReceive)
        {
            if (String.IsNullOrEmpty(userReceive))
                return "";
            int index = IndexManager.FindStoreIndex(userReceive.Trim());
            Account acc = AccountDAO.GetAccountByUsername(userReceive.Trim());
            if (acc != null)
            {
                String friend = "'Username':'{0}','State':'U',";
                JArray arr = new JArray();
                if (acc.Friend != "")
                    arr = JArray.Parse(acc.Friend);
                JObject obj = JObject.Parse('{' + String.Format(friend, id.Trim()).Trim() + '}');
                arr.Add(obj);
                if (index != -1)
                    StateController.AddToStore(index, id, "requestF");
                String json = IndexManager.ListtoStringJSon("", arr);
                acc.Friend = json;
                AccountDAO.UpdateAccount(acc.Username, acc);
                return "OK";
            }
            else return "NotExist";
        }

        // POST api/Friend/username?userreceive
        public String PostFriendAccept(String id, [FromUri]String userReceive, [FromUri]bool accept)
        {
            int index = IndexManager.FindStoreIndex(userReceive.Trim());
            Account acc = AccountDAO.GetAccountByUsername(id.Trim());
            if (acc != null)
            {
                JArray arr = new JArray();
                if (acc.Friend != "")
                    arr = JArray.Parse(acc.Friend);
                foreach (var o in arr)
                {
                    String username = o["Username"].ToString().Trim();
                    if (username.CompareTo(userReceive.Trim()) == 0)
                    {
                        String state = o["State"].ToString().Trim();
                        if (state == "U")
                        {
                            if (accept)
                            {
                                o["State"] = "A";
                                Account a = AccountDAO.GetAccountByUsername(userReceive.Trim());
                                if (a != null)
                                {
                                    String friend = "'Username':'{0}','State':'A',";
                                    JArray ar = new JArray();
                                    if (a.Friend != "")
                                        ar = JArray.Parse(a.Friend);
                                    JObject obj = JObject.Parse('{' + String.Format(friend, id.Trim()).Trim() + '}');
                                    ar.Add(obj);
                                    String js = IndexManager.ListtoStringJSon("", ar);
                                    a.Friend = js;
                                    AccountDAO.UpdateAccount(a.Username, a);
                                }
                                if (index != -1)
                                    StateController.AddToStore(index, id, "acceptF");
                            }
                            else o["State"] = "N";
                        }
                        break;
                    }
                }
                String json = IndexManager.ListtoStringJSon("", arr);
                acc.Friend = json;
                AccountDAO.UpdateAccount(acc.Username, acc);
                return "OK";
            }
            else return "NotExist";
        }
    }
}
